package cn.shop.front.service.impl;

import cn.shop.base.MyPage;
import cn.shop.front.entity.order.SimpleOrder;
import cn.shop.front.entity.req.GameOrderQueryReq;
import cn.shop.front.entity.req.OrderQueryReq;
import cn.shop.front.mapper.SimpleOrderMapper;
import cn.shop.front.service.OrderService;
import cn.shop.front.util.CurrentUserUtil;
import cn.shop.utils.PageUtil;
import cn.shop.utils.Putif;
import cn.shop.utils.enums.GoodsType;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author jzw
 * @since 2021-11-30 09:47
 */
@Service
public class OrderServiceImpl extends ServiceImpl<SimpleOrderMapper, SimpleOrder> implements OrderService {

	@Override
	public MyPage<SimpleOrder> orderList(MyPage<SimpleOrder> page, Long gameId, Long partitionId, GoodsType goodsType, Integer orderByDownTime) {

		return baseMapper.pageWithArea(page, gameId, partitionId, goodsType, orderByDownTime);
	}

	@Override
	public MyPage<SimpleOrder> orderListApp(MyPage<SimpleOrder> page, List<Long> gameIds) {

		return baseMapper.pageWithApp(page, gameIds);
	}

	@Override
	public MyPage<SimpleOrder> orderList(MyPage<SimpleOrder> page, GameOrderQueryReq req) {
		return baseMapper.pageWithGame(page, req);
	}

	/**
	 * 检验当前商品是否可买
	 *
	 * @param orderId 商品id
	 * @author jzw
	 * @since 2022-01-07 15:45:19
	 */
	@Override
	public Boolean checkOrder(Long orderId) {
		return baseMapper.checkOrder(CurrentUserUtil.getUserId(), orderId);
	}

	@Override
	public Map<String, Object> unreceivedList(int current, int size, long userId) {
		Page<SimpleOrder> page = baseMapper.unreceivedOrder(Page.of(current, size), userId);
		return PageUtil.formatPage(page, o ->
				Putif.start()
						.anyway("time", o.getDealTime())
						.anyway("goods", o.getGoods())
						.anyway("goodsNum", o.getGoodsNum())
						.anyway("id", o.getId())
						.anyway("game", o.getGame())
						.anyway("area", o.getArea())
						.anyway("account", o.getBuyerAccount())
						.get());
	}

	@Override
	public MyPage<SimpleOrder> orderList(MyPage<SimpleOrder> page, OrderQueryReq req) {
		return baseMapper.pageWithUser(page, req);
	}
}
